package com.szxx.suit;

import java.io.File;
import java.io.RandomAccessFile;
import java.net.URL;
import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import com.szxx.domain.Card;
import com.szxx.domain.Cards;

public abstract class SuitTestService {
	
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private ISuitAnalysisService analysisService;
	
	@Value("${onlyforTestTypes}")
    protected boolean onlyforTestTypes;
	
	@PostConstruct
	public void init() {
		if (onlyforTestTypes) {
			doSuitTest();
			doTakeApartTest();
			System.exit(0);
		}
	}

	/**
	 * 拆牌测试
	 */
	private void doTakeApartTest() {
		System.out.println("=================================================");
		System.out.println("================ TakeApart START ================");
		System.out.println("=================================================\n\n");
		long start = System.currentTimeMillis();
		int lineNum = 0;
		URL url = SuitTestService.class.getClassLoader().getResource("type/takeapart_test.txt");
		if (url != null) {
			String path = url.getFile();
			File file = new File(path);
			if (file.exists()) {
				try {
					RandomAccessFile accessFile = new RandomAccessFile(file, "r");
					for (;;) {
						String line = accessFile.readLine();
						if (StringUtils.isBlank(line)) {
							break;
						}
						lineNum++;
						Cards testCards = transfer2Cards(line);
						List<AbstractSuitType> types = analysisService.listAbstractSuitType(false);
						if (types == null || types.size() == 0) {
							System.out.println(testCards + " 找不到对应的牌型!!! ");
						} else {
							System.out.println(testCards + " 包含的牌型为：" + types);
							for (AbstractSuitType type : types) {
								System.out.println("-----------" + type.getClass().getSimpleName() + "-----------");
								List<Cards> cardsList = analysisService.listInTypePossibleCards(testCards.clone(), null, type);
								for (Cards cardsTemp : cardsList) {
									System.out.println(cardsTemp);
								}
							}
						}
						System.out.println();
					}
					accessFile.close();
				} catch (Exception e) {
					logger.error("", e);
				}
			}
			if (lineNum > 0) {
				long use =  System.currentTimeMillis() - start;
				double avg = ((int)(1000.0 * use / lineNum)) / 1000.0;
				System.out.println("use time:" + use + "ms for lineNum:" + lineNum 
						+ " avg:" + avg +"ms.");
			}
		}
		System.out.println("\n\n=================================================");
		System.out.println("================= TakeApart END =================");
		System.out.println("=================================================\n\n");
	}

	/**
	 * 牌型测试
	 */
	protected void doSuitTest() {
		System.out.println("=================================================");
		System.out.println("================ SuitTest START =================");
		System.out.println("=================================================\n\n");
		long start = System.currentTimeMillis();
		int lineNum = 0;
		URL url = SuitTestService.class.getClassLoader().getResource("type/suit_test.txt");
		if (url != null) {
			String path = url.getFile();
			File file = new File(path);
			if (file.exists()) {
				try {
					RandomAccessFile accessFile = new RandomAccessFile(file, "r");
					for (;;) {
						String line = accessFile.readLine();
						if (StringUtils.isBlank(line)) {
							break;
						}
						lineNum++;
						Cards testCards = transfer2Cards(line);
						List<AbstractSuitType> types = analysisService.listPossibleTypes(testCards.clone(), null, true);
						if (types == null || types.size() == 0) {
							System.out.println(testCards + " 找不到对应的牌型!!! ");
						} else {
							System.out.println(testCards + " 对应的牌型为：" + types);
							for (AbstractSuitType type : types) {
								System.out.println("-----------" + type.getClass().getSimpleName() + "-----------");
								List<Cards> cardsList = analysisService.analysisLaizi(testCards.clone(), null, type);
								for (Cards cardsTemp : cardsList) {
									System.out.println(cardsTemp);
								}
							}
						}
						System.out.println();
					}
					accessFile.close();
				} catch (Exception e) {
					logger.error("", e);
				}
			}
			if (lineNum > 0) {
				long use =  System.currentTimeMillis() - start;
				double avg = ((int)(1000.0 * use / lineNum)) / 1000.0;
				System.out.println("use time:" + use + "ms for lineNum:" + lineNum 
						+ " avg:" + avg +"ms.");
			}
		}
		System.out.println("\n\n=================================================");
		System.out.println("================= SuitTest END ==================");
		System.out.println("=================================================\n\n");
	}
	
	public Cards transfer2Cards(String cardsStr) {
		Cards cards = new Cards();
		String[] cardStrs = cardsStr.split(",");
		for (String cardStr : cardStrs) {
			if (!StringUtils.isBlank(cardStr)) {
				Card card = transfer2Card(cardStr);
				if (card != null) {
					cards.addCard(transfer2Card(cardStr));
				}
			}
		}
		return cards;
	}

	public abstract Card transfer2Card(String cardStr);
	
	public void main(String[] args) {
	}

}
